package com.ziyueyuwen.xiniao.controller.manage;

import com.alibaba.fastjson.JSONObject;
import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.BaseUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.bean.trial.TrialClassVO;
import com.ziyueyuwen.xiniao.bean.trial.ZyTrialClassDO;
import com.ziyueyuwen.xiniao.service.TrialClassService;
import com.ziyueyuwen.xiniao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.ziyueyuwen.xiniao.base.BaseUtil.checkParam;
import static com.ziyueyuwen.xiniao.enums.ParamTypeEnum.INTEGER_GT_0;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author jun 2018/12/20.
 */
@Controller
@RequestMapping("/manage/trial/class")
public class ManageTrialClassController {
    @Autowired
    private TrialClassService trialClassService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/add_trial_class.do", method = RequestMethod.POST)
    public void addTrialClass(@RequestParam(PARAM_COURSE_ID) Integer courseId,
                              @RequestParam(PARAM_CLASS_NAME) String className,
                              @RequestParam(PARAM_SEAT_NUM) Integer seatNum,
                              @RequestParam(PARAM_MASTER_ACCOUNT) String masterAccount,
                              @RequestParam(PARAM_TEACHER_ACCOUNT) String teacherAccount,
                              @RequestParam(PARAM_START_TIME) Long startTime,
                              @RequestParam(PARAM_END_TIME) Long endTime,
                              HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("添加体验课");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_COURSE_ID, courseId);
            checkParam(INTEGER_GT_0, PARAM_SEAT_NUM, seatNum);
            checkParam(PARAM_CLASS_NAME, className);
            checkParam(PARAM_MASTER_ACCOUNT, masterAccount);
            checkParam(PARAM_TEACHER_ACCOUNT, teacherAccount);
            checkParam(PARAM_START_TIME, startTime);
            checkParam(PARAM_END_TIME, endTime);
            // do.
            ZyUser masterInfo = userService.getByAccount(masterAccount);
            ZyUser teacherInfo = userService.getByAccount(teacherAccount);
            ZyTrialClassDO zyTrialClassDO = new ZyTrialClassDO();
            zyTrialClassDO.setCourseId(courseId);
            zyTrialClassDO.setClassName(className);
            zyTrialClassDO.setMasterAccount(masterAccount);
            zyTrialClassDO.setTeacherAccount(teacherAccount);
            zyTrialClassDO.setStartTime(new Date(startTime));
            zyTrialClassDO.setEndTime(new Date(endTime));
            zyTrialClassDO.setMasterName(masterInfo.getUserName());
            zyTrialClassDO.setTeacherName(teacherInfo.getUserName());
            zyTrialClassDO.setSeatNum(seatNum);
            trialClassService.add(zyTrialClassDO);
            BaseUtil.saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            BaseUtil.saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_trial_class.do", method = RequestMethod.POST)
    public void updateTrialClass(@RequestParam(PARAM_ID) Integer id,
                                 @RequestParam(PARAM_CLASS_NAME) String className,
                                 @RequestParam(PARAM_SEAT_NUM) Integer seatNum,
                                 @RequestParam(PARAM_MASTER_ACCOUNT) String masterAccount,
                                 @RequestParam(PARAM_TEACHER_ACCOUNT) String teacherAccount,
                                 @RequestParam(PARAM_START_TIME) Long startTime,
                                 @RequestParam(PARAM_END_TIME) Long endTime,
                                 HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("编辑体验课");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_ID, id);
            checkParam(INTEGER_GT_0, PARAM_SEAT_NUM, seatNum);
            checkParam(PARAM_CLASS_NAME, className);
            checkParam(PARAM_MASTER_ACCOUNT, masterAccount);
            checkParam(PARAM_TEACHER_ACCOUNT, teacherAccount);
            checkParam(PARAM_START_TIME, startTime);
            checkParam(PARAM_END_TIME, endTime);
            // do.
            ZyUser masterInfo = userService.getByAccount(masterAccount);
            ZyUser teacherInfo = userService.getByAccount(teacherAccount);
            ZyTrialClassDO zyTrialClassDO = new ZyTrialClassDO();
            zyTrialClassDO.setId(id);
            zyTrialClassDO.setClassName(className);
            zyTrialClassDO.setMasterAccount(masterAccount);
            zyTrialClassDO.setTeacherAccount(teacherAccount);
            zyTrialClassDO.setStartTime(new Date(startTime));
            zyTrialClassDO.setEndTime(new Date(endTime));
            zyTrialClassDO.setMasterName(masterInfo.getUserName());
            zyTrialClassDO.setTeacherName(teacherInfo.getUserName());
            zyTrialClassDO.setSeatNum(seatNum);
            trialClassService.update(zyTrialClassDO);
            BaseUtil.saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            BaseUtil.saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/list_trial_class.do", method = RequestMethod.POST)
    public void listTrialClass(@RequestParam(PARAM_PAGE_NUM) Integer pageNum,
                               @RequestParam(PARAM_PAGE_SIZE) Integer pageSize,
                               @RequestParam(PARAM_COURSE_ID) Integer courseId,
                               HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<JSONObject> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取体验课列表");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_COURSE_ID, courseId);
            checkParam(INTEGER_GT_0, PARAM_PAGE_NUM, pageNum);
            checkParam(INTEGER_GT_0, PARAM_PAGE_SIZE, pageSize);
            // do.
            JSONObject retObj = trialClassService.listByCourseId(pageNum, pageSize, courseId);
            BaseUtil.saveSuccessMsgAndData(hrc, retObj);
        } catch (Exception e) {
            BaseUtil.saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_trial_class.do", method = RequestMethod.POST)
    public void getTrialClass(@RequestParam(PARAM_ID) Integer id,
                              HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<TrialClassVO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取体验课");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_ID, id);
            // do.
            TrialClassVO retObj = trialClassService.getById(id);
            BaseUtil.saveSuccessMsgAndData(hrc, retObj);
        } catch (Exception e) {
            BaseUtil.saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/delete_trial_class.do", method = RequestMethod.POST)
    public void deleteTrialClass(@RequestParam(PARAM_ID) Integer id,
                                 HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("删除体验课");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_ID, id);
            // do.
            trialClassService.deleteById(id);
            BaseUtil.saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            BaseUtil.saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }


}
